﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.DataGrid;
using CommunitySystem.EmpbasicService;
using EHR.Common;
using System.Collections.ObjectModel;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpbasicWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private JinShengService.JinShengServiceClient jclient = new JinShengService.JinShengServiceClient();
        private EmpbasicService.Emp_BasicInfoModel model = new EmpbasicService.Emp_BasicInfoModel();
        private EmpbasicService.Emp_BasicInfoModel oldmodel = new EmpbasicService.Emp_BasicInfoModel();
        public int ID;
        public int OrgID;
        public bool IsNew = false;
        private ObservableCollection<EmpbasicService.Emp_BasicInfoModel> selemplist = null;
        public EmpbasicWindow(int id, bool isnew, int orgid1, string status)
        {
            InitializeComponent();
            //科室只有管理员可进行操作
            if (Session.LoginUser.RoleID == 1)
            {
                biaoti.IsEnabled = true;
                this.tbsid.IsEnabled = true;
            }
            else
            {
                biaoti.IsEnabled = false;
                this.tbsid.IsEnabled = false;
            }

            jclient.GetEmpInfoCompleted += new EventHandler<JinShengService.GetEmpInfoCompletedEventArgs>(jclient_GetEmpInfoCompleted);

            orgid.OnSelectionChanged += new OnSelectionChangedEvent(orgid_OnSelectionChanged);
            ID = id;
            IsNew = isnew;
            OrgID = orgid1;
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            Client.InsertCheGaiCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_InsertCheGaiCompleted);
            Client.GetBianZhiModelCompleted += new EventHandler<EmpbasicService.GetBianZhiModelCompletedEventArgs>(Client_GetBianZhiModelCompleted);
            ygzt.OnSelectionChanged += new OnSelectionChangedEvent(ygzt_OnSelectionChanged);
            if (IsNew)
            {
                this.Title = "新增人员基本信息";
                model = new EmpbasicService.Emp_BasicInfoModel();
                model.OrgID = OrgID;
                model.EmpStatus = "在职";
                //this.orgid.IsEnabled = true;
                //name.IsEnabled = true;
                //name.IsEnabled = true;
                this.DataContext = model;

            }
            else
            {
                this.Title = "编辑人员基本信息";

                //this.orgid.IsEnabled = false;
                //name.IsEnabled = false;
                //name.IsEnabled = false;
                Client.GetEmp_BasicInfoCompleted += new EventHandler<EmpbasicService.GetEmp_BasicInfoCompletedEventArgs>(Client_GetEmp_BasicInfoCompleted);
                Client.GetEmp_BasicInfoAsync("0", "(" + ID + ")", "", "", 0, 20, status, "", null, null);
            }
        }

        void Client_GetBianZhiModelCompleted(object sender, EmpbasicService.GetBianZhiModelCompletedEventArgs e)
        {
            biaoti.ItemsSource = null;
            biaoti.ItemsSource = e.Result;
            if (biaoti.ItemsSource != null)
            {
                for (int i = 0; i < ((ObservableCollection<BianZhiModel>)biaoti.ItemsSource).Count; i++)
                {
                    if (((ObservableCollection<BianZhiModel>)biaoti.ItemsSource)[i].JiGouMingCheng == model.PositionName)
                        biaoti.SelectedIndex = i;
                }
            }
        }

        void orgid_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            if (NewValue != null)
            {
                Client.GetBianZhiModelAsync(NewValue.ToString(), 0);
            }
        }

        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                model.PositionName = ((BianZhiModel)((((ComboBox)sender).SelectedItem))).JiGouMingCheng;
            }

        }
        private bool isCardNo = false;
        void jclient_GetEmpInfoCompleted(object sender, JinShengService.GetEmpInfoCompletedEventArgs e)
        {

            if (e.Result > 0)
                isCardNo = true;
            SaveInfo();
        }

        void ygzt_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            if (NewValue.ToString() != "在职")
            {
                zhuangtai.Visibility = System.Windows.Visibility.Visible;
                zhuangtai1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                zhuangtai.Visibility = System.Windows.Visibility.Collapsed;
                zhuangtai1.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            if (e.Result != "人事干部")
            {
                this.orgid.IsEnabled = true;
            }
            else
            {

                if (!IsNew)
                    this.orgid.IsEnabled = false;
            }
        }

        void Client_GetEmp_BasicInfoCompleted(object sender, EmpbasicService.GetEmp_BasicInfoCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                model = e.Result[0];

                if (model.TuiXiuDate != null && Convert.ToDateTime(model.TuiXiuDate) != Convert.ToDateTime("1900-01-01"))
                {
                    tuixiu.Text = Convert.ToDateTime(model.TuiXiuDate).ToString("yyyy-MM");
                    tuixiu12.Text = model.JuTuiXiuDate.ToString();
                }

                //if (!string.IsNullOrEmpty(model.Birthday.ToString()))
                //{
                //    if (model.Gender == "男")
                //    {
                //        tuixiu.Text = Convert.ToDateTime(model.Birthday.ToString()).AddYears(60).Year + "-" +
                //                      Convert.ToDateTime(model.Birthday.ToString()).AddYears(60).Month;
                //        tuixiu12.Text =
                //            (Convert.ToDateTime(model.Birthday.ToString()).AddYears(60).Year - DateTime.Today.Year).ToString() ;
                //    }
                //    if (model.Gender == "女")
                //    {
                //        tuixiu.Text = Convert.ToDateTime(model.Birthday.ToString()).AddYears(55).Year + "-" +
                //                  Convert.ToDateTime(model.Birthday.ToString()).AddYears(55).Month;
                //        tuixiu12.Text =
                //            (Convert.ToDateTime(model.Birthday.ToString()).AddYears(55).Year - DateTime.Today.Year).ToString() ;
                //    }
                //}
                oldmodel = new EmpbasicService.Emp_BasicInfoModel
                {
                    Birthday = model.Birthday,
                    CardNo = model.CardNo,
                    Name = model.Name,
                    Gender = model.Gender,
                    BirthPlace = model.BirthPlace,
                    HouseholdRegister = model.HouseholdRegister,
                    Native = model.Native,
                    Political = model.Political,
                    Health = model.Health,
                    WorkDate = model.WorkDate,
                    RegisterNo = model.RegisterNo,
                    OrganizationType = model.OrganizationType,
                    NowWorkDate = model.NowWorkDate,
                    NativePlace = model.NativePlace,
                    Marriage = model.Marriage,
                    HomeAddress = model.HomeAddress,
                    Phone = model.Phone,
                    EmpIdentity = model.EmpIdentity,
                    PartyTime = model.PartyTime,
                    Special = model.Special,
                    TechnicalPositions = model.TechnicalPositions,
                    PersonnelType = model.PersonnelType,
                    RegisterDate = model.RegisterDate,
                    PositionName = model.PositionName,
                    TuiXiuDate = model.TuiXiuDate,
                    JuTuiXiuDate = model.JuTuiXiuDate,
                    HuoDeFangShi = model.HuoDeFangShi,
                    OrgID = model.OrgID,
                    EmpStatus = model.EmpStatus,
                    StatusDate = model.StatusDate
                };
            }
            this.DataContext = model;
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(name.Text))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入人员姓名！");
                return;
            }
            if (string.IsNullOrEmpty(cardno.Text))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入人员证件号码！");
                return;
            }
            if (!ValiIdCard(cardno.Text))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入正确的身份证号码！");
                return;
            }


            jclient.GetEmpInfoAsync(cardno.Text);



        }

        private void SaveInfo()
        {
            if (isCardNo && IsNew == true)
            {
                isCardNo = false;
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员的身份证号系统中已存在！");
                return;

            }

            if (oldmodel.OrgID != model.OrgID && (oldmodel.OrgID != null && oldmodel.OrgID != 0))
            {
                MessageBox.Show("您已修改该人员单位！");
                Client.InsertCheGaiAsync(model.ID, "更改人员单位", "变动表");
            }
            if (oldmodel.EmpStatus != model.EmpStatus && !string.IsNullOrEmpty(oldmodel.EmpStatus))
            {
                MessageBox.Show("您已修改该人员的状态！");
            }
            if (zhuangtai.Visibility == System.Windows.Visibility.Visible && string.IsNullOrEmpty(model.StatusDate.ToString()))
            {
                MessageBox.Show("请正确填写身份变更时间！");
                return;
            }
            if (!string.IsNullOrEmpty(tuixiu.Text)) model.TuiXiuDate = Convert.ToDateTime(tuixiu.Text + "-01");
            model.JuTuiXiuDate = Convert.ToInt32(tuixiu12.Text);
            if (IsNew == true)
            {
                Client.AddEmp_BasicInfoCompleted += new EventHandler<EmpbasicService.AddEmp_BasicInfoCompletedEventArgs>(Client_AddEmp_BasicInfoCompleted);
                Client.AddEmp_BasicInfoAsync(model);
            }
            else
            {
                Client.UpdateEmp_BasicInfoCompleted += new EventHandler<EmpbasicService.UpdateEmp_BasicInfoCompletedEventArgs>(Client_UpdateEmp_BasicInfoCompleted);
                Client.UpdateEmp_BasicInfoAsync(model);
            }
        }

        void Client_InsertCheGaiCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {

        }

        void Client_UpdateEmp_BasicInfoCompleted(object sender, EmpbasicService.UpdateEmp_BasicInfoCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");

                InsertSystem("编辑");
                this.DialogResult = true;
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("已存在该姓名+证件号码的人员，请检查！");
            }
        }

        private InsertSystemRemark insertsys = new InsertSystemRemark();
        void Client_AddEmp_BasicInfoCompleted(object sender, EmpbasicService.AddEmp_BasicInfoCompletedEventArgs e)
        {
            if (e.Result > 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                InsertSystem("新增");
                this.DialogResult = true;
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("已存在该姓名+证件号码的人员，请检查！");
            }
        }
        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("人员基本信息", "", "", model.ID, FangShi, "");
            }
            else
            {
                if (oldmodel.Birthday != model.Birthday)
                    insertsys.Insert("人员基本信息", oldmodel.Birthday == null ? null : Convert.ToDateTime(oldmodel.Birthday).ToShortDateString(),
                        model.Birthday == null ? null : Convert.ToDateTime(model.Birthday).ToShortDateString(),
                        model.ID, FangShi, "生日");
                if (oldmodel.BirthPlace != model.BirthPlace)
                    insertsys.Insert("人员基本信息", oldmodel.BirthPlace, model.BirthPlace,
                        model.ID, FangShi, "出生地");
                if (oldmodel.CardNo != model.CardNo)
                    insertsys.Insert("人员基本信息", oldmodel.CardNo, model.CardNo,
                        model.ID, FangShi, "证件号码");
                if (oldmodel.EmpIdentity != model.EmpIdentity)
                    insertsys.Insert("人员基本信息", oldmodel.EmpIdentity, model.EmpIdentity,
                        model.ID, FangShi, "个人身份");
                if (oldmodel.Gender != model.Gender)
                    insertsys.Insert("人员基本信息", oldmodel.Gender, model.Gender,
                        model.ID, FangShi, "性别");
                if (oldmodel.Health != model.Health)
                    insertsys.Insert("人员基本信息", oldmodel.Health, model.Health,
                        model.ID, FangShi, "健康状况");
                if (oldmodel.HomeAddress != model.HomeAddress)
                    insertsys.Insert("人员基本信息", oldmodel.HomeAddress, model.HomeAddress,
                        model.ID, FangShi, "家庭住址");
                if (oldmodel.HouseholdRegister != model.HouseholdRegister)
                    insertsys.Insert("人员基本信息", oldmodel.HouseholdRegister, model.HouseholdRegister,
                        model.ID, FangShi, "户籍所在地");
                if (oldmodel.Marriage != model.Marriage)
                    insertsys.Insert("人员基本信息", oldmodel.Marriage, model.Marriage,
                        model.ID, FangShi, "婚姻状况");
                if (oldmodel.Name != model.Name)
                    insertsys.Insert("人员基本信息", oldmodel.Name, model.Name,
                        model.ID, FangShi, "姓名");
                if (oldmodel.Native != model.Native)
                    insertsys.Insert("人员基本信息", oldmodel.Native, model.Native,
                        model.ID, FangShi, "民族");
                if (oldmodel.NativePlace != model.NativePlace)
                    insertsys.Insert("人员基本信息", oldmodel.NativePlace, model.NativePlace,
                        model.ID, FangShi, "籍贯");
                if (oldmodel.NowWorkDate != model.NowWorkDate)
                    insertsys.Insert("人员基本信息", oldmodel.NowWorkDate == null ? null : Convert.ToDateTime(oldmodel.NowWorkDate).ToShortDateString(),
                        model.NowWorkDate == null ? null : Convert.ToDateTime(model.NowWorkDate).ToShortDateString(),
                        model.ID, FangShi, "进本单位年月");
                if (oldmodel.OrganizationType != model.OrganizationType)
                    insertsys.Insert("人员基本信息", oldmodel.OrganizationType, model.OrganizationType,
                        model.ID, FangShi, "编制类型");
                if (oldmodel.PartyTime != model.PartyTime)
                    insertsys.Insert("人员基本信息", oldmodel.PartyTime == null ? null : Convert.ToDateTime(oldmodel.PartyTime).ToShortDateString(),
                        model.PartyTime == null ? null : Convert.ToDateTime(model.PartyTime).ToShortDateString(),
                        model.ID, FangShi, "进本单位年月");
                if (oldmodel.PersonnelType != model.PersonnelType)
                    insertsys.Insert("人员基本信息", oldmodel.PersonnelType, model.PersonnelType,
                        model.ID, FangShi, "人员类别");
                if (oldmodel.Phone != model.Phone)
                    insertsys.Insert("人员基本信息", oldmodel.Phone, model.Phone,
                        model.ID, FangShi, "住宅电话");
                if (oldmodel.Political != model.Political)
                    insertsys.Insert("人员基本信息", oldmodel.Political, model.Political,
                        model.ID, FangShi, "政治面貌");
                if (oldmodel.PositionName != model.PositionName)
                    insertsys.Insert("人员基本信息", oldmodel.PositionName, model.PositionName,
                        model.ID, FangShi, "现部门名称");
                if (oldmodel.RegisterDate != model.RegisterDate)
                    insertsys.Insert("人员基本信息", oldmodel.RegisterDate == null ? null : Convert.ToDateTime(oldmodel.RegisterDate).ToShortDateString(),
                        model.RegisterDate == null ? null : Convert.ToDateTime(model.RegisterDate).ToShortDateString(),
                        model.ID, FangShi, "公务员登记时间");
                if (oldmodel.RegisterNo != model.RegisterNo)
                    insertsys.Insert("人员基本信息", oldmodel.RegisterNo, model.RegisterNo,
                        model.ID, FangShi, "公务员登记号");
                if (oldmodel.Special != model.Special)
                    insertsys.Insert("人员基本信息", oldmodel.Special, model.Special,
                        model.ID, FangShi, "专长");
                if (oldmodel.WorkDate != model.WorkDate)
                    insertsys.Insert("人员基本信息", oldmodel.WorkDate == null ? null : Convert.ToDateTime(oldmodel.WorkDate).ToShortDateString(),
                        model.WorkDate == null ? null : Convert.ToDateTime(model.WorkDate).ToShortDateString(),
                        model.ID, FangShi, "参加工作日期");
                if (oldmodel.TuiXiuDate != model.TuiXiuDate)
                    insertsys.Insert("人员基本信息", oldmodel.TuiXiuDate == null || string.IsNullOrEmpty(oldmodel.TuiXiuDate.ToString()) ? null : Convert.ToDateTime(oldmodel.TuiXiuDate).ToShortDateString(),
                        model.TuiXiuDate == null ? null : Convert.ToDateTime(model.TuiXiuDate).ToShortDateString(),
                        model.ID, FangShi, "退休时间");
                if (oldmodel.JuTuiXiuDate != model.JuTuiXiuDate)
                    insertsys.Insert("人员基本信息", oldmodel.JuTuiXiuDate.ToString(), model.JuTuiXiuDate.ToString(),
                        model.ID, FangShi, "距离退休年限");
                if (oldmodel.HuoDeFangShi != model.HuoDeFangShi)
                    insertsys.Insert("人员基本信息", oldmodel.HuoDeFangShi, model.HuoDeFangShi,
                        model.ID, FangShi, "任本区公务员身份方式");
                if (oldmodel.OrgID != model.OrgID)
                    insertsys.Insert("人员基本信息", oldmodel.OrgID.ToString(), model.OrgID.ToString(),
                        model.ID, FangShi, "现工作单位");
                if (oldmodel.EmpStatus != model.EmpStatus)
                    insertsys.Insert("人员基本信息", oldmodel.EmpStatus, model.EmpStatus,
                        model.ID, FangShi, "人员状态");
                if (oldmodel.StatusDate != model.StatusDate)
                    insertsys.Insert("人员基本信息", oldmodel.StatusDate == null ? null : Convert.ToDateTime(oldmodel.StatusDate).ToShortDateString(),
                        model.StatusDate == null ? null : Convert.ToDateTime(model.StatusDate).ToShortDateString(),
                        model.ID, FangShi, "身份变更日期");
            }

        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public void SetStatus(string idcard)
        {
            if (ValiIdCard(idcard))
            {
                model.Gender = Checks.IsMan(idcard) ? "男" : "女";
                gender.Field = "Gender";
                model.Birthday = Convert.ToDateTime(Checks.GetBirthDay(idcard).ToString());
                Change(model.Birthday.ToString());
            }


        }
        private void textBoxCode2_LostFocus(object sender, EventArgs e)
        {
            SetStatus(cardno.Text);
        }
        /// <summary>
        /// 验证字符串是否为空或者只包含空格
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsStringMissing(string value)
        {
            return
                String.IsNullOrEmpty(value) ||
                value.Trim() == String.Empty;
        }
        /// <summary>
        /// 验证身份证号码
        /// </summary>
        /// <param name="idCard"></param>
        /// <returns></returns>
        public static bool ValiIdCard(string idcard)
        {
            if (IsStringMissing(idcard)) return false;
            if (idcard == null || (idcard.Length != 15 && idcard.Length != 18))
            {
                return false;
            }

            if (idcard.Length == 18)
            {
                System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x|X)$");
                System.Text.RegularExpressions.Match mc = rg.Match(idcard);
                if (!mc.Success)
                {
                    return false;
                }

                if (int.Parse(idcard.Substring(6, 4)) < 1850 || int.Parse(idcard.Substring(6, 4)) > DateTime.Now.Year + 1)
                {
                    return false; // 不允许 1850年以前的日期，以及不允许当前年＋2以后的日期

                }

                try
                {
                    DateTime.Parse(idcard.Substring(6, 4) + "-" + idcard.Substring(10, 2) + "-" + idcard.Substring(12, 2));
                }
                catch
                {
                    return false;
                }

                string idcard15 = idcard.Substring(0, 6) + idcard.Substring(8, 9);

                //if (GetLongIDCardNumber(idcard15) != idcard)
                //{
                //    return false;
                //}

            }
            else if (idcard.Length == 15)
            {
                System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{15}");
                System.Text.RegularExpressions.Match mc = rg.Match(idcard);
                if (!mc.Success)
                {
                    return false;
                }

                try
                {
                    DateTime.Parse("19" + idcard.Substring(6, 2) + "-" + idcard.Substring(8, 2) + "-" + idcard.Substring(10, 2));
                }
                catch
                {
                    return false;
                }
            }

            string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,
											 "北京","天津","河北","山西","内蒙古",null,null,null,null,null,
											 "辽宁","吉林","黑龙江",null,null,null,null,null,null,null,
											 "上海","江苏","浙江","安微","福建","江西","山东",null,null,null,
											 "河南","湖北","湖南","广东","广西","海南",null,null,null,
											 "重庆","四川","贵州","云南","西藏",null,null,null,null,null,null,
											 "陕西","甘肃","青海","宁夏","新疆",null,null,null,null,null,
											 "台湾",null,null,null,null,null,null,null,null,null,
											 "香港","澳门",null,null,null,null,null,null,null,null,"国外"};
            if (aCity[int.Parse(idcard.Substring(0, 2))] == null)
            {
                return false;
            }

            return true;
        }
        public static string GetLongIDCardNumber(string idcard)
        {
            //		计算方法
            //		(1)十七位数字本体码加权求和公式
            //			   S = Ai * Wi, i = 2, ... , 18
            //		Y = mod(S, 11)
            //		i: 表示号码字符从右至左包括校验码字符在内的位置序号
            //			   Ai:表示第i位置上的身份证号码字符值

            //					  Wi:表示第i位置上的加权因子
            //							 i: 18 17 16 15 14 13 12 11 10 9 8 7  6 5 4 3 2 1
            //		Wi: 7  9 10  5  8  4  2  1  6 3 7 9 10 5 8 4 2 1
            //		(2)校验码字符值的计算
            //			   Y:      0 1 2 3 4 5 6 7 8 9 10
            //		校验码: 1 0 X 9 8 7 6 5 4 3  2

            if (!ValiIdCard(idcard))
            {
                return null;
            }

            if (idcard.Length == 18)
            {
                return idcard;
            }

            idcard = idcard.Substring(0, 6) + "19" + idcard.Substring(6, 9);

            int[] Wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
            string[] CK = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };

            int sig = 0;
            int i;

            for (i = 0; i < 17; i++)
            {
                sig += Wi[i] * int.Parse(idcard.Substring(i, 1));
            }
            int y = sig % 11;

            for (i = 0; i < 11; i++)
            {
                if (y == i)
                {
                    idcard += CK[i];
                }
            }

            return idcard;
        }
        private void Change(string text)
        {
            if (!string.IsNullOrEmpty(text))
            {
                if (model.Gender == "男")
                {
                    tuixiu.Text = Convert.ToDateTime(text).AddYears(60).Year + "-" +
                                  Convert.ToDateTime(text).AddYears(60).Month;
                    tuixiu12.Text =
                        (Convert.ToDateTime(text).AddYears(60).Year - DateTime.Today.Year).ToString();
                }
                if (model.Gender == "女")
                {
                    tuixiu.Text = Convert.ToDateTime(text).AddYears(55).Year + "-" +
                              Convert.ToDateTime(text).AddYears(55).Month;
                    tuixiu12.Text =
                        (Convert.ToDateTime(text).AddYears(55).Year - DateTime.Today.Year).ToString();
                }
            }
        }
        private void TextBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            Change(((DatePicker)sender).Text);
        }

        private void jiguan_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("SelectItem") && jiguan.SelectItem != null)
            {
                this.tbjiguan.Visibility = Visibility.Collapsed;
            }
        }

    }
    public class Checks
    {
        //是否男

        public static bool IsMan(string id)
        {
            if (id.Length == 18)
                return (int.Parse(id.Substring(16, 1)) % 2) == 1 ? true : false;
            return (int.Parse(id.Substring(14, 1)) % 2) == 1 ? true : false;
        }

        //获取生日
        public static DateTime GetBirthDay(string id)
        {
            if (id.Length == 18)
            {
                var birthday = id.Substring(6, 4) + "-" + id.Substring(10, 2) + "-" + id.Substring(12, 2);
                return Convert.ToDateTime(birthday);
            }
            else
            {
                var birthday = id.Substring(6, 2) + "-" + id.Substring(8, 2) + "-" + id.Substring(10, 2);
                return Convert.ToDateTime(birthday);
            }
        }

    }
}

